package com.huluobo.day11_im.adapter;

import android.text.format.DateUtils;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.day11_im.R;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;

import java.util.Date;
import java.util.List;

public class ConversationListAdapter extends BaseQuickAdapter<EMConversation, BaseViewHolder> {
    public ConversationListAdapter(int layoutResId, List<EMConversation> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, EMConversation emConversation) {
        baseViewHolder.setText(R.id.item_conversationId, emConversation.conversationId());
        String contentText = "文本";
        if (emConversation.getLastMessage().getType() == EMMessage.Type.TXT) {
            contentText =
                    ((EMTextMessageBody) emConversation.getLastMessage().getBody()).getMessage();
        } else if (emConversation.getLastMessage().getType() == EMMessage.Type.FILE) {
            contentText = "[文件]";
        } else if (emConversation.getLastMessage().getType() == EMMessage.Type.IMAGE) {
            contentText = "[图片]";
        }
        baseViewHolder.setText(R.id.item_lastMessage, contentText);
        baseViewHolder.setText(R.id.timestamp,
                DateUtils.getRelativeTimeSpanString(emConversation.getLastMessage().getMsgTime()));

        if (emConversation.getUnreadMsgCount() > 0) {
            baseViewHolder.setVisible(R.id.item_unreadCount, true);
            baseViewHolder.setText(R.id.item_unreadCount, emConversation.getUnreadMsgCount()+"");
        } else {
            baseViewHolder.setGone(R.id.item_unreadCount, true);
        }
    }
}
